var api = require('../../../utils/api.js')
var $ = require('../../../utils/util.js')
var regeneratorRuntime = require('../../../utils/runtime.js')
var app = getApp()
var Store = app.Store 

Page({
  data: {
    pageList: [], //分页数据列表
    rejectReason:'',
    isLose:false,
    isPass:false,
    signImg:''
  },

  onLoad: function (options) {
    if ('ticketNumber' in options){ 
      this.setData({
        ticketNumber:options.ticketNumber, 
        type:options.type,
      })
      this.getTicketInfo()
    }
  },
  onShow: function () {
    var form = Store.getState().formInfor.form
    var key = form.key
    console.log(form)
    switch (key) {
      case 'workSign': // 获取签发人签名  
        this.setData({
          signImg: form.selected,
        })
        break 
    
      default:
        this.setData({
          signImg: wx.getStorageSync('signUrl'),
        })
        break;
    }
    console.log(this.data.signImg)
  },
  beginSign(e){  
    var that=this
    if(!Boolean(that.data.signImg)){
      Store.dispatch({
        type: 'SET_FROM',
        form: {
          key: 'workSign', 
        },
      })
      $.linkurl(`/pages/components/signatrue/signAture?ticketNumber=${that.data.ticketNumber}`) 
    }
    // wx.showModal({
    //   title: '提示',
    //   content: '使用原有签名或者手动签名',
    //   confirmText:'原有签名',
    //   cancelText:'手动签名',
    //   success (res) {
    //     if (res.confirm) {
          
    //       that.setData({
    //         signImg: wx.getStorageSync('signUrl'),
    //       })
    //     } else if (res.cancel) {
    //       Store.dispatch({
    //         type: 'SET_FROM',
    //         form: {
    //           key: 'workSign', 
    //         },
    //       })
    //       $.linkurl(`/pages/components/signatrue/signAture?ticketNumber=${that.data.ticketNumber}`) 
    //     }
    //   }
    // })
  },
  bigSign(e){
    const {img}=$.detail(e)
    wx.previewImage({
      current: '', // 当前显示图片的链接，不填则默认为urls的第一张
      urls: [img] // 需要预览的图片链接列表
    })
  },
  //##input 输入##
  input_handle(e) {
    this.setData({
      [e.currentTarget.dataset.key]: e.detail.value
    })
  },
  reject(){
    this.setData({ isLose: true }) 
  },
  pass(){
    this.setData({ isPass: true }) 
  },
  setReject(){
    if($.isNull(this.data.rejectReason)){
      $.toast("驳回原因不能为空")
        return
    }
    if(!Boolean(this.data.signImg)){
      return $.toast('当前无默认签名，请手动填写！')
    }
    if(this.data.type=='签发人确认'){
      api.$get(api.workTicket.workLeaderChangeQFR, {
        ticketNumber:this.data.ticketNumber,
        approvalResult:false,
        signature:this.data.signImg,
        approvalRemark:this.data.rejectReason
      }).then((res) => {
        this.getTicketInfo()
      })
    }
    if(this.data.type=='许可人确认'){
      api.$get(api.workTicket.workLeaderChangeXKR, {
        ticketNumber:this.data.ticketNumber,
        approvalResult:false,
        signature:this.data.signImg,
        approvalRemark:this.data.rejectReason
      }).then((res) => {
        this.getTicketInfo()
      })
    }
  },
  setPass(){
    if(!Boolean(this.data.signImg)){
      return $.toast('当前无默认签名，请手动填写！')
    }
    if(this.data.type=='签发人确认'){
      api.$get(api.workTicket.workLeaderChangeQFR, {
        ticketNumber:this.data.ticketNumber,
        approvalResult:true,
        signature:this.data.signImg,
      }).then((res) => {
        this.getTicketInfo()
      })
    }
    if(this.data.type=='许可人确认'){
      api.$get(api.workTicket.workLeaderChangeXKR, {
        ticketNumber:this.data.ticketNumber,
        approvalResult:true,
        signature:this.data.signImg,
      }).then((res) => {
        this.getTicketInfo()
      })
    }
  },
  getTicketInfo(){
    api.$get(api.workTicket.workTickInfo+this.data.ticketNumber).then((res) => {
      this.setData({
        pageList:res.data.personnelChangesList
      })
    })
  },
})